package com.luozongyao.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luozongyao.commonutils.R;
import com.luozongyao.eduService.entity.EduTeacher;
import com.luozongyao.eduService.entity.vo.TeacherQuery;
import com.luozongyao.eduService.service.EduTeacherService;


import com.luozongyao.servicebase.config.exceptionHandler.lzyException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-07
 */
@RestController
@RequestMapping("/eduService/edu-teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    //查询讲师表中所有数据
    @ApiOperation(value = "查询讲师表中所有数据")
    @GetMapping("/findAll")
    public R findAll(){
        List<EduTeacher> teachers = eduTeacherService.list(null);
        return  R.ok().data("items",teachers);
    }

    //2.逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除讲师的方法")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if (flag==true){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //3.分页查询讲师的方法
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,@PathVariable long limit){
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current,limit);

        try {
            int i = 1/0;
        } catch (Exception e) {
            throw new lzyException(20001,"执行了自定义异常处理");
        }

        //调用方法实现分类
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(teacherPage,null);
        long totalCount = teacherPage.getTotal();//总记录数
        List<EduTeacher> records = teacherPage.getRecords(); //每页记录数
        return R.ok().data("totalCount",totalCount).data("records",records);
    }
    //使用requestBody get请求不到 将json数据传递到对应对象里 required = false 表示后面数据可以没有
    //4.条件查询带分页的方法
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //mybatis学过 动态sql
        //判断条件是否为空 如果不为空拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        //begin end 用于时间范围查找
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        eduTeacherService.page(page,wrapper);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return R.ok().data("total",total).data("records",records);

    }

    //5.添加讲师接口的方法
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save==true){
            return R.ok();
        }else {
            return R.error();
        }
    }


    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("getTeacherById/{id}")
    public R getById(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

